<?php namespace App\Http\Controllers\user\uc\saver;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Session;
use App\Models\EnterpriceEnergysaverIndustry;
use App\Models\Techs;
use App\Models\Domains;
use App\Models\SaverDomain;
use App\Models\SaverTech;
use Input;
use Validator;

class IndustryInfoController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{

		$uid = $this->getUid();
		$model = EnterpriceEnergysaverIndustry::with(["saverDomain", "saverTech", "saverDomain.domain", "saverDomain.domain.parent", "saverTech.techs"])->where('uid', $uid);
		if($model->count()){
			$data = $model->get()[0]->toArray();
		}else{
			$data = [];
		}
		// dd($data);
		return view('user.uc.saver.industryinfo', ['data' => $data])->withIsSaverIndustry(true);
	}

	public function editIndustryinfo()
	{
		$techs = Techs::all()->toArray();
		$domains = Domains::where("parent_id", 0)->get()->toArray();
		return view('user.uc.saver.edit_industryinfo', ["techs" => $techs, "domains" => $domains])->withIsSaverIndustry(true);
	}

	// 后台用户列表

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	public function updateIndustryinfo()
	{
		$rule = array(
			// 'company_name'	=>	'required'
			);
		$msg = array();
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes()){
			$uid = $this->getUid();
			$model = EnterpriceEnergysaverIndustry::where('uid', $uid);
			if($model->count()){
				$data = Input::except('domains', 'techs');
				$imgArr = Input::only("bank_credit_copy");
				$this->moveImages($imgArr, env("OTHERS_DIR"));
				$saver_id = $model->get()[0]->id;
				$model->update($data);
				$domain_model = SaverDomain::where("saver_id", $saver_id);
				$domain_model->delete();
				$tech_model = SaverTech::where("saver_id", $saver_id);
				$tech_model->delete();

				$techs = Input::get("techs");
				$domains = Input::get("domains");
				foreach ($techs as $tech)
				{
					SaverTech::create(["cate_id" => $tech, "saver_id" => $saver_id]);
				}
				foreach ($domains as $domain)
				{
					SaverDomain::create(["cate_id" => $domain, "saver_id" => $saver_id]);
				}
				return $this->json([]);
			}
		}else{
			$errors = $validator->messages();
			return self::error($error->first());
		}
	}

	public function industryinfoList()
	{
		$uid = $this->getUid();
		$model = EnterpriceEnergysaverIndustry::with(["saverTech", "saverDomain", "saverDomain.domain"])->where('uid', $uid);
		if($model->count()){
			$data = $model->get()[0]->toArray();
			return $this->json(["data" => $data]);
		}else{
			return self::error(self::NO_DATA);
		}
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
}
